<template>
  <div class="app-container">
    <el-form ref="form" :model="form" label-width="120px">
      <el-form-item label="题目">
        <el-input v-model="form.title"/>
      </el-form-item>
      <el-form-item label="题型">
        <el-select v-model="form.type" placeholder="请选择题型" @change="show">
          <el-option label="选择题" value="1"/>
          <el-option label="解答题" value="2"/>
        </el-select>
      </el-form-item>

      <div :class="{hid:hidden!=1}">
        <el-form-item label="A:">
          <el-input v-model="form.option1"/>
        </el-form-item>
        <el-form-item label="B:">
          <el-input v-model="form.option2"/>
        </el-form-item>
        <el-form-item label="C:">
          <el-input v-model="form.option3"/>
        </el-form-item>
        <el-form-item label="D:">
          <el-input v-model="form.option4"/>
        </el-form-item>
        <el-form-item label="正确答案">
          <el-radio-group v-model="form.rightOption">
            <el-radio :label="1">A</el-radio>
            <el-radio :label="2">B</el-radio>
            <el-radio :label="3">C</el-radio>
            <el-radio :label="4">D</el-radio>
          </el-radio-group>
        </el-form-item>
      </div>
      <div :class="{hid:hidden!=2}">
        <el-form-item label="参考答案">
          <el-input v-model="form.suggestAnswer" type="textarea"/>
        </el-form-item>
      </div>
      <el-form-item>
        <el-button type="primary" @click="onSubmit">添加</el-button>
        <!-- <el-button @click="onCancel">Cancel</el-button> -->
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { createItem } from '@/api/item.js'
export default {
  data() {
    return {
      form: {
        title: '',
        option1: '',
        option2: '',
        option3: '',
        option4: '',
        rightOption: '',
        suggestAnswer: ''
      },
      hidden: 0
    }
  },
  methods: {
    onSubmit() {
      if (this.form.type === 1) {
        this.form.suggestAnswer = ''
      } else if (this.form.type === 2) {
        this.form.option1 = ''
        this.form.option2 = ''
        this.form.option3 = ''
        this.form.option4 = ''
        this.form.rightOption = ''
      }

      createItem(this.form)

      this.$message('submit!')
    },
    onCancel() {
      this.$message({
        message: 'cancel!',
        type: 'warning'
      })
    },
    show(value) {
      this.hidden = value
    }
  }
}
</script>

<style scoped>
.line {
  text-align: center;
}
.hid {
  display: none;
}
</style>

